/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package da;

import entities.FineLoan;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bich Tran Van
 */
public class DAFineLoan {

    MSSQLConnection conn = null;

    public DAFineLoan() {
        try {
            conn = new MSSQLConnection();
            conn.getConnection();
        } catch (Exception ex) {
            Logger.getLogger(DALoanType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // get all loan type
    public ArrayList<FineLoan> getAll() throws Exception {
        ArrayList<FineLoan> list = new ArrayList<FineLoan>();
        try {
            conn.getConnection();
            conn.cstm = conn.connect.prepareCall("{CALL View_all_FineLoan}");
            conn.rs = conn.cstm.executeQuery();
            while (conn.rs.next()) {
                FineLoan fineLoan = new FineLoan();
                fineLoan.setFineId(conn.rs.getInt("FineID"));
                fineLoan.setToMoney(conn.rs.getDouble("ToMoney"));
                fineLoan.setFromMoney(conn.rs.getDouble("FromMoney"));
                fineLoan.setFine(conn.rs.getFloat("Fine"));
                list.add(fineLoan);
            }
            conn.myClose();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //get loan type by id
    public ArrayList<FineLoan> getByID(int id) throws Exception {
        ArrayList<FineLoan> list = new ArrayList<FineLoan>();
        try {
            conn.getConnection();
            conn.cstm = conn.connect.prepareCall("{CALL View_FineLoan_By_ID(?)}");
            conn.cstm.setInt(1, id);
            conn.rs = conn.cstm.executeQuery();
            while (conn.rs.next()) {
                FineLoan fineLoan = new FineLoan();
                fineLoan.setFineId(conn.rs.getInt("FineID"));
                fineLoan.setToMoney(conn.rs.getDouble("ToMoney"));
                fineLoan.setFromMoney(conn.rs.getDouble("FromMoney"));
                fineLoan.setFine(conn.rs.getFloat("Fine"));
                list.add(fineLoan);
            }
            conn.myClose();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //insert to Loan type
    public int insert(double toMoney, double fromMoney, float fine) throws Exception {
        int result = 0;
        try {
            conn.getConnection();
            conn.cstm = conn.connect.prepareCall("{ CALL Insert_FineLoan(?,?,?) }");
            conn.cstm.setDouble(1, toMoney);
            conn.cstm.setDouble(2, fromMoney);
            conn.cstm.setFloat(3, fine);
            result = conn.cstm.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    //update to loan type
    public int update(FineLoan fineLoan) throws Exception {
        int result = 0;
        try {
            conn.getConnection();
            conn.cstm = conn.connect.prepareCall("{CALL Update_FineLoan(?,?,?,?)}");
            conn.cstm.setInt(1, fineLoan.getFineId());
            conn.cstm.setDouble(2, fineLoan.getToMoney());
            conn.cstm.setDouble(3, fineLoan.getFromMoney());
            conn.cstm.setDouble(4, fineLoan.getFine());
            result = conn.cstm.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    //delete from loan type
    public int delete(int id) throws Exception {
        int result = 0;
        try {
            conn.getConnection();
            conn.cstm = conn.connect.prepareCall("{CALL Delete_FineLoan(?)}");
            conn.cstm.setInt(1, id);
            result = conn.cstm.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}
